import $ from 'jquery'
import { prefix } from '../config/api'

export const get = url => request(url)

export const post = (url, data = {}) => request(url,'post', data)

export const put = (url, data = {}) => request(url, 'put', data)

export const del = (url, data = {}) => request(url, 'delete', data)

function request(url, type = 'get', data = {}, header = {}) {
  let requestConfig = {
    url: prefix + url,
    type,
    headers: {
      ...header,
      token: sessionStorage.getItem('token') || ''
    }
  }
  if (type != 'get') requestConfig = { ...requestConfig, data }
  return $.ajax(requestConfig)
}